// pages/user/myAddress/myAddress.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        hasMore: true,
        data: [],
        defaultId: 0,
        isChoose: 0,
        isLongTap: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.isChoose = options.is_choose;
        this.onPullDownRefresh();
        wx.emitter.on('address.update', this.onPullDownRefresh);
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function(options) {
        wx.emitter.off('address.update', this.onPullDownRefresh);
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.loadData(1).finally(() => {
            wx.stopPullDownRefresh();
        });
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        // if (!this.data.hasMore) return;
        //
        // const page = this.data.page + 1;
        // this.loadData(page);
    },

    // 加载数据列表
    loadData: function(page) {
        return wx.http.post('api/address/lists', {}).then((res) => {
            res = res.data.data;
            const data = page > 1 ? this.data.data.concat(res.list) : res.list;
            // if (data.length === 1 && data[0].address_id === res.default_id) {
            //     wx.emitter.emit('address.choose', data[0]);
            // }
            this.setData({
                // page: page,
                // hasMore: hasMore,
                defaultId: res.default_id,
                data: data,
            });
            return res.data;
        }, (err) => {
            console.warn("api/address/lists", err)
        });
    },

    // 删除地址
    deleteTap: function(e) {
        const index = e.currentTarget.dataset.index;
        const item = this.data.data[index];

        wx.showModal({
            content: '删除之后将不能恢复，你确定要删除吗？',
            showCancel: true,
            success: (res) => {
                if (res.cancel) return;

                wx.http.post('api/address/delete', {
                    address_id: item.address_id
                }).then((res) => {
                    const data = this.data.data;
                    data.splice(index, 1);
                    const key = `data[${index}]`;
                    this.setData({
                        data: data
                    })
                });
            }
        })
    },

    // 选择地址
    chooseAddress: function(e) {
        if (this.data.isLongTap) {
            this.data.isLongTap = false;
            return;
        }
        if (this.data.isChoose !== '1') return;
        const index = e.currentTarget.dataset.index;
        const item = this.data.data[index];
        wx.emitter.emit('address.choose', item);
        wx.navigateBack({
            delta: 1
        })
    },

    // 更新默认地址
    setDefaultChange: function(e) {
        console.log(e);
        const index = e.detail.value;
        const item = this.data.data[index];
        return wx.http.post('api/address/setDefault', {
            address_id: item.address_id
        }, {
            showLoading: true
        }).then((res) => {
            this.setData({
                defaultId: item.address_id
            });

            if (this.data.isChoose === '1') {
                // wx.emitter.emit('address.choose', item);
                // wx.navigateBack({
                //     delta: 1
                // });
            }
        });
    },

    // 设置默认地址
    setDefaultTap: function(e) {
        this.data.isLongTap = true;
        const index = e.currentTarget.dataset.index;
        const item = this.data.data[index];

        wx.promise.showActionSheet({
            itemList: [
                "设置默认地址"
            ],
        }).then((res) => {
            return wx.http.post('api/address/setDefault', {
                address_id: item.address_id
            }, {
                showLoading: true
            })
        }).then((res) => {
            this.setData({
                defaultId: item.address_id
            });

            if (this.data.isChoose === '1') {
                // wx.emitter.emit('address.choose', item);
                // wx.navigateBack({
                //     delta: 1
                // });
            }
        });
    }
});